import pygame
from lab import * #@UnusedWildImport
import math

class GraphDang(dang):
        
    def __init__(self):
        self._minimap = None
        dang.__init__(self)
                
    def generate(self):
        self._minimap = None
        dang.generate(self)
        
    def draw(self,width,hight,color):
        g = self._gabarits()
        self.basesize = min(1.0*width / (g[1][0]-g[0][0]),1.0*hight / (g[1][1]-g[0][1]))    
        screen = pygame.Surface((width,hight))
        scale = lambda x,y:(x*self.basesize,y*self.basesize)
        centering = lambda x,y:scale(x-g[0][0]+1,y-g[0][1]+1)
        for r in self.rooms:
            pygame.draw.rect(screen,color,pygame.Rect(centering(r.x,r.y),scale(r.w,r.h)))  
        bl = int(round(self.basesize))
        if bl == 0 : bl=1      
        for c in self.coridors:    
            pygame.draw.lines(screen,(color),False,[centering(x,y) for (x,y) in c],bl)
        return screen
    
    def draw_minimap(self,width,hight,color = (0,0,255)):
        if self._minimap != None: return self._minimap
        self._minimap = self.draw(width, hight, color)
        return self._minimap
    

class World():
    
    def __init__(self,basesize=8):
        self.dang = GraphDang()
        self.basesize = basesize
        self.hight = 0
        self.width = 0
        self.map = None

    def make(self):
        self.dang.generate()
        g = self.dang._gabarits()
        self.width = (g[1][0]-g[0][0])*self.basesize
        self.hight = (g[1][1]-g[0][1])*self.basesize
        ts = self.dang.draw(self.width, self.hight, (255,50,50))
        self.map = pygame.mask.from_threshold(ts, (255,50,50),(10,10,10,255))
        rec = self.map.get_bounding_rects()
        return rec
        
        
        
    def getvisible(self,x,y,w=700,h=540):
        mask = pygame.Mask((w, h))
        mask.fill()
        res = mask.overlap_mask(self.map, (-x,-y))
        view = pygame.Surface((w,h))
        for i in range(w):
            for j in range(h):
                if res.get_at((i,j)):
                    view.set_at((i,j),(0,0,255))
        return view

           


def newDang():
    pygame.init()
    resolution = (800, 640)
    screen = pygame.display.set_mode(resolution)
    pygame.display.set_caption("Random Dungeon Generator")


    clock = pygame.time.Clock()
    fps = 25


    background = pygame.surface.Surface(resolution)
    background.fill((100, 200, 50))
  
    dg = GraphDang()
    dg.generate()


    fn = pygame.font.SysFont('Arial', 12)
    running = True
    while running:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
                elif event.key == pygame.K_SPACE:
                    del dg.coridors[:]
                    del dg.rooms[:]
                    dg.generate()




        keypressed = pygame.key.get_pressed()
        if keypressed[pygame.K_w] or keypressed[pygame.K_UP]:
            pass


        if keypressed[pygame.K_s] or keypressed[pygame.K_DOWN]:
            pass
        #if keypressed[pygame.K_a] or keypressed[pygame.K_LEFT]:
         
        #if keypressed[pygame.K_d] or keypressed[pygame.K_RIGHT]:


#        screen.blit(background, (0, 0))
        l = dg.draw_minimap(100, 100)
        screen.blit(l,(5,5))
                
        # for r in dg.rooms:
            # fl = pygame.surface.Surface((r.w*8,r.h*8))
            # fl.fill((0,0,255))
            # screen.blit(fl, (r.x*8, r.y*8))
        
        # for c in dg.coridors:    
            # pygame.draw.lines(screen,(0,0,255),False,[(x*8,y*8) for (x,y) in c],8)
            
        s = str(dg.str())
        
        screen.blit(fn.render(s,True,(255,0,255)),(10,10))
        pygame.display.flip()    
    pygame.quit()

def testWorld():
    pygame.init()
    resolution = (800, 640)
    screen = pygame.display.set_mode(resolution)
    pygame.display.set_caption("Random Dungeon Generator")


    clock = pygame.time.Clock()
    fps = 25


    background = pygame.surface.Surface(resolution)
    background.fill((100, 200, 50))
  
    world = World(10)
    world.make()
    viewx, viewy = 0, 0
    fn = pygame.font.SysFont('Arial', 14)
    running = True
    while running:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
                elif event.key == pygame.K_SPACE:
                    pass




        keypressed = pygame.key.get_pressed()
        if keypressed[pygame.K_w] or keypressed[pygame.K_UP]:
            viewy-=5


        if keypressed[pygame.K_s] or keypressed[pygame.K_DOWN]:
            viewy+=5
        if keypressed[pygame.K_a] or keypressed[pygame.K_LEFT]:
            viewx-=5

        if keypressed[pygame.K_d] or keypressed[pygame.K_RIGHT]:
            viewx+=5


#        screen.blit(background, (0, 0))
        l = world.getvisible(viewx, viewy, 400, 400)
        screen.blit(l,(5,5))
        m = world.dang.draw_minimap(100, 100, (255,0,0))
        screen.blit(m,(700,500))
        screen.blit(fn.render("x:"+str(viewx)+" y:"+str(viewy),True,(255,0,255)),(10,10))
        pygame.display.flip()    
    pygame.quit()


if __name__ == "__main__":
    testWorld()